<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="160px"
    >
      <el-form-item label="计划编号" prop="planNo">
        <el-input
          v-model="queryParams.planNo"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="计划开始时间" prop="planStartDate">
        <el-date-picker
          clearable
          v-model="queryParams.planStartDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="计划结束时间" prop="planEndDate">
        <el-date-picker
          clearable
          v-model="queryParams.planEndDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item label="电压等级" prop="voltageLevel">
        <el-select
          v-model="queryParams.voltageLevel"
          placeholder="请选择"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dl_voltage_level"
            :key="dict.label"
            :label="dict.label"
            :value="dict.label"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="作业组织（建管）单位" prop="jobGroupName">
        <el-input
          v-model="queryParams.jobGroupName"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工作负责人" prop="workHeaderName">
          <el-input
            v-model="queryParams.workHeaderName"
            placeholder="请输入"
            :disabled="formDisable"
          />
        </el-form-item>

      <el-form-item>
        <el-button @click="handleAdd">新增</el-button>
        <el-button @click="handleDelete">批量删除</el-button>
        <el-button @click="handleQuery">查询</el-button>
        <el-button @click="resetQuery">重置</el-button>
        <el-button @click="handleExport">导出</el-button>
        <el-button @click="handleImport">导入</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="planList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="作业组织（建管）单位"
        align="center"
        prop="jobGroupName"
      />
      <el-table-column label="电压等级" align="center" prop="voltageLevel" />
      <el-table-column
        label="作业风险等级"
        align="center"
        prop="jobRiskLevel"
      />
      <el-table-column
        label="电网风险等级"
        align="center"
        prop="powerGridRiskLevel"
      />
      <el-table-column label="作业类型" align="center" prop="jobType" />
      <el-table-column label="工作负责人" align="center" prop="workHeaderName" />
      <el-table-column
        label="计划开始时间"
        align="center"
        prop="planStartDate"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.planStartDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="计划结束时间"
        align="center"
        prop="planEndDate"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.planEndDate) }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="作业内容" align="center" prop="iobContent" /> -->
      <el-table-column label="计划编号" align="center" prop="planNo" />
      <el-table-column label="计划状态" align="center" prop="planStatus" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding"
        width="200"
      >
        <template #default="scope">
          <el-button
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['platform:plan:edit']"
            >修改</el-button
          >
          <el-button
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['platform:plan:remove']"
            >删除</el-button
          >
          <el-button type="text" @click="handleLook(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改异动计划对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="planRef" :model="form" :rules="rules" label-width="160px">
        <el-form-item label="作业组织（建管）单位" prop="jobGroupUnit">
          <el-tree-select
            v-model="form.jobGroupName"
            :data="deptOptions"
            :props="{ value: 'label', label: 'label', children: 'children' }"
            value-key="label"
            placeholder="请选择"
            check-strictly
            :disabled="formDisable" 
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="电压等级" prop="voltageLevel">
          <el-select
            v-model="form.voltageLevel"
            placeholder="电压等级"
            clearable
            :disabled="formDisable" 
            style="width: 100%"
          >
            <el-option
              v-for="dict in dl_voltage_level"
              :key="dict.label"
              :label="dict.label"
              :value="dict.label"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="作业风险等级" prop="jobRiskLevel">
          <el-select
            v-model="form.jobRiskLevel"
            placeholder="作业风险等级"
            clearable
            :disabled="formDisable" 
            style="width: 100%"
          >
            <el-option
              v-for="dict in dl_job_risk_level"
              :key="dict.label"
              :label="dict.label"
              :value="dict.label"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="电网风险等级" prop="powerGridRiskLevel">
          <el-select
            v-model="form.powerGridRiskLevel"
            placeholder="电网风险等级"
            clearable
            :disabled="formDisable" 
            style="width: 100%"
          >
            <el-option
              v-for="dict in dl_power_grid_risk_level"
              :key="dict.label"
              :label="dict.label"
              :value="dict.label"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="作业类型" prop="powerGridRiskLevel">
          <el-select
            v-model="form.jobType"
            placeholder="作业类型"
            clearable
            :disabled="formDisable" 
            style="width: 100%"
          >
            <el-option
              v-for="dict in dl_job_type"
              :key="dict.label"
              :label="dict.label"
              :value="dict.label"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="工作负责人" prop="workHeaderName">
          <el-input
            v-model="form.workHeaderName"
            placeholder="请输入工作负责人"
            :disabled="formDisable"
          />
        </el-form-item>
        <el-form-item label="计划开始时间" prop="planStartDate">
          <el-date-picker
            clearable
            v-model="form.planStartDate"
            type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择计划开始时间"
            :disabled="formDisable" 
            style="width: 100%"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="计划结束时间" prop="planEndDate">
          <el-date-picker
            clearable
            v-model="form.planEndDate"
            type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择计划结束时间"
            :disabled="formDisable" 
            style="width: 100%"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="作业内容">
          <editor
            v-if="!formDisable"
            v-model="form.iobContent"
            :min-height="192"
          />
          <div class="ql-editor" v-else v-html="form.iobContent"></div>
        </el-form-item>
        <el-form-item label="计划编号" prop="planNo">
          <el-input
            v-model="form.planNo"
            placeholder="请输入计划编号"
            :disabled="formDisable"
          />
        </el-form-item>
        <el-form-item label="计划状态" prop="planStatus">
          <el-select
            v-model="form.planStatus"
            placeholder="计划状态"
            clearable
            :disabled="formDisable" 
            style="width: 100%"
          >
            <el-option
              v-for="dict in dl_plan_status"
              :key="dict.label"
              :label="dict.label"
              :value="dict.label"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm" v-if="!formDisable"
            >确 定</el-button
          >
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 展示弹出层 -->
    <div>
      <ImportChangePlanDialog
        v-model="importChangePlanVisible"
        @refresh="handleQuery"
      ></ImportChangePlanDialog>
    </div>
  </div>
</template>

<script setup name="Plan">
import {
  listPlan,
  getPlan,
  delPlan,
  addPlan,
  updatePlan,
} from "@/api/platform/changePlan";

import { deptTreeSelect } from "@/api/system/dept";

import ImportChangePlanDialog from "@/views/changePlanManage/changePlan/dialog/ImportChangePlanDialog.vue";

const { proxy } = getCurrentInstance();
const {
  dl_voltage_level,
  dl_job_risk_level,
  dl_power_grid_risk_level,
  dl_job_type,
  dl_plan_status,
} = proxy.useDict(
  "dl_voltage_level",
  "dl_job_risk_level",
  "dl_power_grid_risk_level",
  "dl_job_type",
  "dl_plan_status"
);

const planList = ref([]);
const open = ref(false);
const formDisable = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const deptOptions = ref(undefined);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    jobGroupUnit: null,
    voltageLevel: null,
    jobRiskLevel: null,
    powerGridRiskLevel: null,
    jobType: null,
    workHeader: null,
    workHeaderName: null,
    planStartDate: null,
    planEndDate: null,
    iobContent: null,
    planNo: null,
    planStatus: null,
    isDelete: null,
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);

/** 查询部门下拉树结构 */
function getDeptTree() {
  deptTreeSelect().then((response) => {
    deptOptions.value = response.data;
  });
}

/** 查询异动计划列表 */
function getList() {
  loading.value = true;
  listPlan(queryParams.value).then((response) => {
    planList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  formDisable.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    jobGroupUnit: null,
    voltageLevel: null,
    jobRiskLevel: null,
    powerGridRiskLevel: null,
    jobType: null,
    workHeader: null,
    workHeaderName: null,
    planStartDate: null,
    planEndDate: null,
    iobContent: null,
    planNo: null,
    planStatus: null,
    isDelete: null,
  };
  proxy.resetForm("planRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  formDisable.value = false;
  title.value = "添加异动计划";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getPlan(_id).then((response) => {
    form.value = response.data;
    open.value = true;
    formDisable.value = false;
    title.value = "修改异动计划";
  });
}

/** 查看按钮操作*/
function handleLook(row) {
  const _id = row.id || ids.value;
  getPlan(_id).then((response) => {
    form.value = response.data;
    open.value = true;
    formDisable.value = true;
    title.value = "查看异动计划";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["planRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updatePlan(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addPlan(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除异动计划编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delPlan(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "platform/changePlan/export",
    {
      ...queryParams.value,
    },
    `plan_${new Date().getTime()}.xlsx`
  );
}

getDeptTree();
getList();

const importChangePlanVisible = ref(false);
const handleImport = () => {
  importChangePlanVisible.value = true;
};
</script>
